//
//  LZAudioTool.m
//  QQ音乐
//
//  Created by 1 on 15/11/22.
//  Copyright © 2015年 xiaomage. All rights reserved.
//

#import "LZAudioTool.h"
#import <AVFoundation/AVFoundation.h>

@interface LZAudioTool()

/** 播放器对象 */
@property (nonatomic ,strong) AVAudioPlayer *player;


/** 当前播放的路径 */
@property(nonatomic ,copy) NSURL *currentPlayURL;

@end

@implementation LZAudioTool

- (void)playAudioWith:(NSString *)fileName
{
    // 根据名称, 获取资源路径
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil];
    
    if (fileURL == nil) {
        return;
    }
    
    
    
    if ([self.currentPlayURL.absoluteString isEqualToString:fileURL.absoluteString]) {
        [self.player play];
        return;
    }
    self.currentPlayURL = fileURL;
    
    

    // 根据资源路径, 创建播放器对象
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    
    
    // 开始播放
    [self.player prepareToPlay];
    [self.player play];
    
}

- (void)pauseAudio
{
    [self.player pause];
}

- (void)stopAudio
{
    [self.player stop];
    self.player = nil;
}


@end
